package com.turing.post.organization.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.VoucherTransferOrderDetail;
import com.turing.post.organization.util.VoucherTransferOrderDetailBusiness;
import com.turing.post.organization.util.VoucherTransferOrderDetailDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherTransferOrderDetailBusinessImpl")
@Remote
@Local
public class VoucherTransferOrderDetailBusinessImpl implements
		VoucherTransferOrderDetailBusiness {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 新增凭证调拨单明细
	 * 
	 * @param VoucherTransferOrderDetail
	 *            凭证调拨单明细
	 * @throws PCPSApplicationException
	 */
	@EJB(mappedName = "organization.impl.VoucherTransferOrderDetailDaoImpl")
	private VoucherTransferOrderDetailDao voucherTransferOrderDetailDaoImpl;// 凭证入库单明细

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;// 日志

	public void insertVoucherTransferOrderDetail(PCPSWebInput webInput,
			VoucherTransferOrderDetail voucherTransferOrderDetail)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 调拨单ID
		if (voucherTransferOrderDetail.getTransferOrderId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"调拨单ID为空!");
		}
		// 顺序号
		if (voucherTransferOrderDetail.getSeqNo() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"顺序号为空!");
		}
		// 凭证种类ID
		if (voucherTransferOrderDetail.getVoucherId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证种类ID不能等于0!");
		}
		// 起始凭证号
		if (PCPSUtil.isEmpty(voucherTransferOrderDetail.getStartVoucherNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"起始凭证号为空!");
		}
		// 终止凭证号
		if (PCPSUtil.isEmpty(voucherTransferOrderDetail.getEndVoucherNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"终止凭证号为空!");
		}
		// 验证凭证号长度是否合法
		// Voucher
		// voucher=voucherDaoImpl.getVoucherByVoucherId(voucherTransferOrderDetail.getVoucherId());
		// if(voucher==null) {
		// throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
		// "凭证种类不合法!");
		// }else{
		// if(voucher.getVoucherNoLen()==new
		// BigInteger(String.valueOf(voucherTransferOrderDetail.getStartVoucherNo().length()))){
		// throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
		// "起始凭证号长度为:"+voucher.getVoucherNoLen().toString());
		// }
		// if(voucher.getVoucherNoLen()==new
		// BigInteger(String.valueOf(voucherTransferOrderDetail.getEndVoucherNo().length()))){
		// throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
		// "终止凭证号长度为:"+voucher.getVoucherNoLen().toString());
		// }
		// }
		// 张数
		if (voucherTransferOrderDetail.getCount() < 1) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"张数不能小于1!");
		}
		// 插入数据
		voucherTransferOrderDetailDaoImpl
				.insertVoucherTransferOrderDetail(voucherTransferOrderDetail);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "凭证入库单明细",
				voucherTransferOrderDetail, SysOperLog.operType_Add,
				"StartVoucherNo:"
						+ voucherTransferOrderDetail.getStartVoucherNo()
						+ "EndVoucherNo"
						+ voucherTransferOrderDetail.getEndVoucherNo());
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, VoucherTransferOrderDetail obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
